#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "air32f10x.h"
#include "st7735v.h"
#include "board.h"

#define GPIO_GROUP_TEST GPIOB
#define GPIO_MODE_TEST GPIO_Mode_Out_PP
#define GPIO_SPEED_TEST GPIO_Speed_50MHz
#define GPIO_PIN1_TEST GPIO_Pin_2
#define GPIO_PIN2_TEST GPIO_Pin_10
#define GPIO_PIN3_TEST GPIO_Pin_11

USART_TypeDef *USART_TEST = USART1;

void UART_Configuration(unsigned char usart_num,uint32_t buad);
void GPIO_Configuration(void);

char str[100];
// void printf_s(char *str)
// {
// 	char temp[100];
// 	sprintf(temp,)


// }
/********************************************************************************/
// GPIO输出测试，开发板自带的3个灯依次亮起并熄灭实现跑马灯效果，日志通过串口1发送，波特率为115200

/*****************************************
 * 20240208-003
 * 此工程仅用于Bootloader相关功能，不涉及其他功能
 * 20240208-002
 * 增加bsp.c/.h相关描述，硬件抽象层与硬件解耦，涉及硬件最高层为对应的bsp_xx.c/.h，
 * 上层通过硬件抽象层操作底层
 * 各类外设置于硬件层，通过硬件抽象层调用，可参考HAL库
 * 区分软硬件I2C/SPI/UART
 * 连接脚本增加0x4000偏移地址，方便Bootloader功能使用
 * 20230704-001
 * BUG:1.flash.从文件中的SetStrt()需要注释-----重要-----
 *     2.delay.c/.h文件与底层文件目录同级
/********************************************************************************/
int main(void)
{

	RCC_ClocksTypeDef clocks;
	Delay_Init();				//延时初始化
	UART_Configuration(1,115200); //默认为串口1，波特率115200
	RCC_GetClocksFreq(&clocks); //获取系统时钟频率
    GPIO_Configuration();

	uint16_t a = 255;
    ST7735V_Init();	
	while (1)
	{
		luat_lcd_fill(0x001f);
		// luat_lcd_fill(0xf800);
		// luat_lcd_fill(0x07e0);
        // USART_SendData(USART1, a);

		// GPIO_SetBits(GPIO_GROUP_TEST, GPIO_PIN1_TEST);
		// Delay_Ms(100);
		// GPIO_SetBits(GPIO_GROUP_TEST, GPIO_PIN2_TEST);
		// Delay_Ms(100);
		// GPIO_SetBits(GPIO_GROUP_TEST, GPIO_PIN3_TEST);
		// Delay_Ms(100);
		// GPIO_ResetBits(GPIO_GROUP_TEST, GPIO_PIN1_TEST);
		// Delay_Ms(100);
		// GPIO_ResetBits(GPIO_GROUP_TEST, GPIO_PIN2_TEST);
		// Delay_Ms(100);
		// GPIO_ResetBits(GPIO_GROUP_TEST, GPIO_PIN3_TEST);
		// Delay_Ms(100);
	}
}

void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟

	GPIO_InitStructure.GPIO_Pin = GPIO_PIN1_TEST | GPIO_PIN2_TEST | GPIO_PIN3_TEST;
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_TEST; //速度50MHz
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_TEST;	 //输出模式
	GPIO_Init(GPIO_GROUP_TEST, &GPIO_InitStructure); //初始化GPIOB.2,10,11
}

//UART_Configuration(unsigned char usart_num,uint32_t bound)
void UART_Configuration(unsigned char usart_num,uint32_t buad)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// if (usart_num == (unsigned char)USART_0)
    // {
    //     USART0_Init(buad);
    // }
    // if (usart_num == (unsigned char)USART_1)
    // {
    //     USART1_Init(buad);
    // }
    // else if (usart_num == (unsigned char)USART_2)
    // {
    //     USART2_Init(buad);
    // }





	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	USART_InitStructure.USART_BaudRate = buad;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	USART_Init(USART_TEST, &USART_InitStructure);
	USART_Cmd(USART_TEST, ENABLE);
}

int SER_PutChar(int ch)
{
	while (!USART_GetFlagStatus(USART_TEST, USART_FLAG_TC))
		;
	USART_SendData(USART_TEST, (uint8_t)ch);

	return ch;
}

int fputc(int c, FILE *f)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	if (c == '\n')
	{
		SER_PutChar('\r');
	}
	return (SER_PutChar(c));
}
